<?php

//后台的配置文件
include(dirname(__FILE__).'/config.php');



$dopost = isset($_REQUEST['dopost']) ? trim($_REQUEST['dopost']) : 'list';


/***************************************************************************/
if( 'add' == $dopost ){

	$tid = isset($_GET['tid']) ? intval($_GET['tid']):0;
	//默认值
	$art = array();
	$art['id'] =  0 ;
	$art['tid'] =  1 ;
	
	$art['name'] =  '';
	$art['title'] =  '' ;

	$art['flag'] =  1  ;//多选框 不选择没有值  
	$art['exptime'] =  time() ; //=> string '1483249065' (length=10)

	$art['body'] =  '' ;
	$art['expbody'] =  '' ;
	
	


	$action = '添加';
	include('temp/ad_upt.php');
/***************************************************************************/
}else if( 'upt' == $dopost ){
	
	$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
	//读取当前文章
	$sql = "select * from `ad` where id='{$id}'";
	$art = dbRow($sql);
	//var_dump($art);
	
	


	$action = '修改';
	include('temp/ad_upt.php');
/***************************************************************************/
}else if( 'save' == $dopost ){
	// addsave  uptsave 


	$id = isset($_POST['id']) ? intval($_POST['id']) : 0;

	//var_dump($_POST);	
	$art = array();
	$art['id'] =  intval($_POST['id']) ;
	$art['tid'] =  intval($_POST['tid']) ;
	
	$art['name'] =  trim($_POST['name']) ;

	$art['title'] =  trim($_POST['title']) ;

	$art['flag'] =  isset($_POST['flag']) ? intval($_POST['flag']) : 1  ;//多选框 不选择没有值  
	$art['exptime'] =  strtotime($_POST['exptime']) ; //=> string '1483249065' (length=10)

	$art['body'] =  trim($_POST['body']) ;
	$art['expbody'] =  trim($_POST['expbody']) ;




	//数据验证... 
	if($art['title'] == '' ){ die('{"ret":1,"msg":"文章标题不能为空！！！"}') ;}
	if($art['tid'] == 0 ){ die('{"ret":1,"msg":"请选择所属栏目！！！"}') ;}
	
	
	

	sec($art);// 递归的 sql 转义字符过滤 
	if($id){
		//修改
		$action = '修改';
		unset($art['name']);
		$rs = dbUpdate('ad',$art," id = '{$id}'"); // 表 数据 条件

	}else{
		//添加
		$action = '添加';
		$rs = dbInsert('ad',$art); // 表 数据 
	}

//sleep(3);
	if($rs){
		//msgBox($action.'成功!!!' ,'?t='.time() , 2 );
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = $action.'成功!';
		$j['url'] = '?tid='.$art['tid'].'&t='.time() ;

		die( json_encode($j) );

	}else{
		//msgBox($action.'失败!!!' ,'?t='.time() , 10 );
		
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '写入错误!';

		die( json_encode($j) );

	}



/***************************************************************************/
}else if( 'del' == $dopost ){
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

	 die('{"ret":999,"msg":"禁止删除！！！"}') ;

	//真删除
	$rs = dbDelete('ad'," id = '{$id}'"); // 表 数据 条件
	


	if($rs){
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = '删除成功!!!';
		//$j['url'] = '?tid='.$art['tid'].'&t='.time() ;
		die( json_encode($j) );

	}else{
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '删除失败!!!';
		die( json_encode($j) );
	}
/***************************************************************************/
}else if( 'batch_del' == $dopost ){
	die('{"ret":999,"msg":"功能禁用!!!"}');
	$id_arr = $_POST['id'];
	
	foreach($id_arr as $k => $v){
		$art = array();
		$art['recycle'] =  1 ;
		$rs = dbUpdate('ad',$art," id = '{$k}'"); // 表 数据 条件
	}
	
	die('{"ret":0,"msg":"批量 删除 成功!!!"}');
	

/***************************************************************************/
}else if( 'batch_move' == $dopost ){
	$tid = isset($_POST['movetid']) ? intval( $_POST['movetid'] ) : 0 ;
	$id_arr = $_POST['id'];
	
	//如果tid = 0  
	if( !$tid ){die('{"ret":1,"msg":"请选择移动到哪个栏目!!!"}');}
	
	//如果tid 不存在！！！
	$sql = "select tid from `ad_type` where tid = {$tid}"; 
	if(!dbRow($sql)){ die('{"ret":2,"msg":"栏目tid数据错误!!!"}'); };
	
	foreach($id_arr as $k => $v){
		$art = array();
		$art['tid'] =  $tid ;
		$rs = dbUpdate('ad',$art," id = '{$k}'"); // 表 数据 条件
	}
	
	die('{"ret":0,"msg":"批量 【移动】 成功!!!"}');
	



/***************************************************************************/
}else if( 'list' == $dopost ){

	$tid = isset($_GET['tid']) ? intval($_GET['tid']):0;  //  1
	//条件过来 是否 有 tid
    if( $tid ){
	    $where_tid = "and a.tid = {$tid} ";
    }else{
        $where_tid = '';
    }
    
    


    $sql = "select a.*  , b.typename   
    from `ad` as a left join `ad_type` as b on a.tid = b.tid 
    where 1   $where_tid 
    order by sort asc ,id desc ";


    //var_dump($sql);

    $dl = new ListPage();
    $dl->pageSize = 50;//每页显示多少条记录
    $dl->SetParameter('tid',$tid); //向 页码url中添加其他参数
    $dl -> SetSql($sql);
    $list = $dl ->GetList(); //获取 list 数组2
    $page = $dl ->GetPage(); //获取 分页代码*/


	include('temp/ad_list.php');
/***************************************************************************/
}else{
	die('404');
}
/***************************************************************************/


function typelistOption( $select_id = 0 ){

	$typelist = dbList("select * from `ad_type` where 1 order by sort asc , tid asc ");
	
	$html = '';
	foreach($typelist as $k => $v ){
		$v['_selected'] = ($select_id == $v['tid'])?' selected ':'';
		$html .=" <option value=\"{$v['tid']}\"  {$v['_selected']} > {$v['typename']}</option> ";
	}
	return $html;
}